package com.wo.cloud.auth.config;

import com.wo.cloud.auth.service.impl.UserDetailServiceImpl;
import com.wo.cloud.auth.sms.SmsCodeAuthenticationProvider;
import com.wo.cloud.auth.sms.SmsCodeAuthenticationSecurityConfig;
import com.wo.cloud.security.config.IgnoreUrlPropsConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/6 23:26
 * Description:
 */

@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailServiceImpl userDetailService;

    @Resource
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private IgnoreUrlPropsConfiguration ignoreUrlPropsConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭请求伪造
        http.csrf().disable();
        http.formLogin();
        http.authorizeRequests()
                .antMatchers("/auth/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                .csrf().disable();
    }

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config
//                = http.requestMatchers().anyRequest()
//                .and()
//                .formLogin()
//                .and()
//                .apply(smsCodeAuthenticationSecurityConfig)
//                .and()
//                .authorizeRequests();
//
//        ignoreUrlPropsConfig.getUrls().forEach(url -> {
//            config.antMatchers(url).permitAll();
//        });
//        System.out.println("-----ignoreSecurity"+ ignoreUrlPropsConfig.getIgnoreSecurity());
//        ignoreUrlPropsConfig.getIgnoreSecurity().forEach(url -> {
//            config.antMatchers(url).permitAll();
//        });
//        config
//                任何请求
//                .anyRequest()
//                都需要身份认证
//                .authenticated()
//                csrf跨站请求
//                .and()
//                .csrf().disable();
//    }


    /**
     * 在内存中配置信息
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("admin")
//                .password(passwordEncoder.encode("root"))
//                .authorities("sys:admin");

        auth.userDetailsService(userDetailService)
                .and()
                .authenticationProvider(smsAuthenticationProvider());
    }


    /**
     * 使用密码授权模式不需要登录，内部需要一个认证管理器
     *
     * @return
     * @throws Exception
     */
    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    /**
     * 如果不配置SpringBoot会自动配置一个AuthenticationManager,覆盖掉内存中的用户
     */
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Bean
    public SmsCodeAuthenticationProvider smsAuthenticationProvider() {
        return new SmsCodeAuthenticationProvider(userDetailService);
    }
}
